package com.jn.erp.web.api.common.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.security.entity.SysUser;
import com.jn.erp.security.mapper.SysUserMapper;
import com.jn.erp.web.api.common.entity.TLog;
import com.jn.erp.web.api.common.mapper.TLogMapper;
import com.jn.erp.web.api.common.service.TLogService;
import com.jn.erp.web.api.course.entity.TCourseOrder;
import com.jn.erp.web.api.course.mapper.TCourseOrderMapper;
import com.jn.erp.web.api.course.service.TCourseOrderService;
import com.jn.erp.web.api.student.entity.Student;
import com.jn.erp.web.api.student.mapper.StudentMapper;
import com.jn.erp.web.api.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wyp
 * @since 2024-09-18
 */
@Service
public class TLogServiceImpl extends ServiceImpl<TLogMapper, TLog> implements TLogService {

    @Autowired
    private TCourseOrderMapper tCourseOrderMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    public String saveOrderUpdate( TLog tLog){

        TCourseOrder tCourseOrder = tCourseOrderMapper.selectById(tLog.getTId());
        String studentName;
        if(CollectionUtils.isEmpty(tLog.getStudentIds())){
            Student student = studentMapper.selectById(tCourseOrder.getStudentId());
            studentName = student.getName() ;
        }else{
            StringBuffer studentNames = new StringBuffer();
            List<Student> students = studentMapper.selectList(new QueryWrapper<Student>().in("id", tLog.getStudentIds()));
            students.stream().forEach(u->{studentNames.append(u.getName()).append(",");});
            if (studentNames.length() > 0) {
                studentNames.setLength(studentNames.length() - 1); // 移除最后一位字符
            }
            studentName = studentNames.toString();

        }


        tLog.setOrderNo(tCourseOrder.getOrderNo());
        StringBuffer description = new StringBuffer();
        switch (tLog.getType()){
            case 1: description.append("原到课信息 : ").append("学员姓名：").append(studentName).append(" ").append("到课状态：")
                    .append(getState(tCourseOrder.getState())).append(" ").append("扣课时数：").append(tCourseOrder.getClassHourPay()).append(" ")
                    .append("备注：").append(Objects.isNull(tCourseOrder.getRemark())?"":tCourseOrder.getRemark() ).append("\n")
                    .append("调整为 : ").append("学员姓名：").append(studentName).append(" ").append("到课状态：")
                    .append(getState(tLog.getState())).append(" ").append("扣课时数：").append(tLog.getClassHourPay()).append(" ")
                    .append("备注：").append(Objects.isNull(tLog.getRemark())?"":tLog.getRemark() );break;
            case 2:description.append("添加临时学员："+studentName);break;
            case 3:description.append("添加补课学员："+studentName);break;
        }


        tLog.setDescription(description.toString());
       baseMapper.insert(tLog);
        return "S200";
    }

    private String getState(Integer stateStr){
        String state = "";
        switch(stateStr){
            case 1: state = "未开课";break;
            case 2: state = "点名";break;
            case 3: state = "迟到";break;
            case 4: state = "请假";break;
            case 5: state = "未到";break;
        }
        return state;
    }
}
